<template>
  <div class="bottom-menu">
    <CheckButton
      class="select-all"
      @checkBtnClick="checkBtnClick"
      :isChecked="isSelectAll"
    ></CheckButton>
    <span>全选</span>
    <span class="total-price">合计: ¥{{ totalprice }}</span>
    <span class="buy-product">
      <van-button round type="primary" size="normal" color="#ee0a24"
        >去计算 <span v-if="count > 0">({{ count }})</span></van-button
      >
    </span>
  </div>
</template>

<script>
import CheckButton from "@/components/common/checkbutton/CheckButton";

export default {
  name: "BottomBar",
  data() {
    return {
      selcetall: false,
    };
  },
  components: {
    CheckButton,
  },
  computed: {
    totalprice() {
      const cartlist = this.$store.getters.getcart;
      return cartlist
        .filter((item) => {
          return item.checked;
        })
        .reduce((prevalue, item) => {
          return prevalue + item.count * item.newPrice;
        }, 0)
        .toFixed(2);
    },
    count() {
      const cartlist = this.$store.getters.getcart;
      return cartlist
        .filter((item) => {
          return item.checked;
        })
        .reduce((prevalue, item) => {
          return prevalue + item.count;
        }, 0);
    },
    isSelectAll() {
      //判断是否有一个没被选中     有的话就不显示全选
      // if (this.$store.getters.getcart.length == 0) return false; //没有商品时直接返回false
      // return !this.$store.getters.getcart.find((item) => !item.checked);
      return this.$store.getters.getcart.length == 0
        ? false
        : !this.$store.getters.getcart.find((item) => !item.checked);
    },
  },
  methods: {
    checkBtnClick() {
      //点击的时候全选是选中的状态   则全部不选中
      if (this.isSelectAll) {
        this.$store.getters.getcart.forEach((item) => (item.checked = false));
      } else {
        this.$store.getters.getcart.forEach((item) => (item.checked = true));
      }
      this.$emit("allcheck");
    },
  },
};
</script>

<style scoped>
.bottom-menu {
  width: 100%;
  height: 44px;
  background-color: #eee;
  position: fixed;
  bottom: 49px;
  left: 0;
  box-shadow: 0 -2px 3px rgba(0, 0, 0, 0.2);
  font-size: 14px;
  color: #888;
  line-height: 44px;
  padding-left: 35px;
  box-sizing: border-box;
}

.bottom-menu .select-all {
  position: absolute;
  line-height: 0;
  left: 12px;
  top: 13px;
}

.bottom-menu .total-price {
  margin-left: 15px;
  font-size: 16px;
  color: #666;
}

.bottom-menu .buy-product {
  color: #fff;
  text-align: center;
  float: right;
  display: flex;
}
.buy-product button {
  width: 6rem;
}
</style>
